package com.xzq.socket.nio.server;

import com.xzq.socket.nio.ChannelAdapter;
import com.xzq.socket.nio.ChannelHandler;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.charset.Charset;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/18 20:06
 * @Version 1.0.0
 **/
public class NioServerHandler extends ChannelAdapter {
    public NioServerHandler(Selector selector, Charset charset) {
        super(selector, charset);
    }

    @Override
    protected void channelRead(ChannelHandler channelHandler, String s) {
        try {
            System.out.println(channelHandler.getChannel().getLocalAddress() + ": " + s);
            channelHandler.writeAndFlush("收到！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void channelActive(ChannelHandler channelHandler) {
        try {
            System.out.println("连接建立...." + channelHandler.getChannel().getLocalAddress());
            channelHandler.writeAndFlush("hello, I am server ");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
